//
//  SheepLayer.cpp
//  Sheep
//
//  Created by Tran Tho on 9/8/14.
//
//

#include "SheepLayer.h"
#include "GameScene.h"
#include "SimpleAudioEngine.h"
#include "config.h"
#include "Data.h"

bool SheepLayer::init()
{
    if(!Layer::init())
    {
        return false;
    }
    
    
    _size = Director::getInstance()->getWinSize();
    
    count = 0;
    
    score = 0;
    
    type = 0;
    
    listSheep = new Vector<Sheep*>();
    listPic = new Vector<Pic*>();
  //  listXe = new Vector<Xe*>();
    
    addSheep();
    
    lbScore = Label::createWithTTF(std::to_string(score), "Marker Felt.ttf", 45);
    lbScore->setPosition(_size.width / 2 , _size.height * 4/5);
    lbScore->setColor(Color3B(255, 255, 0));
    this->addChild(lbScore,100);
    
    
    return true;
}

void SheepLayer::update(float dt)
{
    if(!GameScene::getInstance()->isGameOver && GameScene::getInstance()->gameStatus == play)
    {
        actionSheep();
        updateXe(1.0);
        updateScore();
    }
    
}

void SheepLayer::updateXe(float vl)
{
    for(auto _xe : listXe)
    {
        float _y = _xe->xe->getPositionX();
        _xe->setVANTOC(vl);
        _y-= vl;
        
        _xe->xe->setPositionX(_y);
        
        _xe->chu->setPositionX(_xe->xe->getPositionX());
        
        for(auto pic : *listPic)
        {
            
            if(_xe->isCollsion(pic) && _xe->get_ID() != pic->get_ID() && _xe->xeStatus == isInit)
            {
                CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(SOUND_HIT);
                GameScene::getInstance()->isGameOver = true;
                DataImage::getInstance()->changeRate(pic->get_ID(), 1000);
                DataImage::getInstance()->writeFile("Data2.csv", 'r');
                break;
            }
            
            if(_xe->isCollsion(pic) && _xe->get_ID() == pic->get_ID() && _xe->xeStatus == isInit)
            {
                
                score++;
                _xe->xeStatus = isMove;
                pic->runAction(MoveTo::create(0.5, Vec2(_xe->xe->getPositionX(), pic->getPositionY() - pic->getContentSize().height / 2)));
                _xe->xe->runAction(MoveTo::create(2.5, Vec2(-_xe->xe->getContentSize().width, _xe->xe->getPositionY())));
                pic->setScale(0.5);
                pic->setVisible(false);
                pic->runAction(MoveTo::create(2.5, Vec2(-_xe->xe->getContentSize().width, pic->getPositionY())));
                for(auto sheep :*listSheep)
                {
                    if(sheep->get_ID() == pic->get_ID() && sheep->sheepStaus == isNormal)
                    {
                    sheep->sheepStaus = isHit;
                    sheep->runAction(MoveTo::create(2.0, Vec2(_size.width + sheep->getContentSize().width, _size.height + sheep->getContentSize().height)));
                        listSheep->eraseObject(sheep);
                    }
                    
                }
                
               
                
            }
        }
        
        
    }
}

void SheepLayer::onEnter()
{
    Layer::onEnter();
    this->scheduleUpdate();
}

void SheepLayer::onExit()
{
    Layer::onExit();
    this->unscheduleUpdate();
    cleanUp();
    this->removeAllChildrenWithCleanup(true);
}

void SheepLayer::addSheep()
{
    auto info = DataImage::getInstance()->randomQuestion();
    
    
    //tao con chim
    auto sheep = Sheep::create();
    sheep->setPosition(_size.width  * 1 / 9, _size.height * 3 / 4);
    sheep->set_ID(info.ID);
    this->addChild(sheep, 1000);
    listSheep->pushBack(sheep);
    CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(SOUND_BEE);
    
    auto pic = Pic::create(info);
    pic->setPosition(sheep->getPositionX(), sheep->getPositionY() - pic->getContentSize().height /2 );
    //pic->setScale(0.5);
    this->addChild(pic);
    listPic->pushBack(pic);
    
    if(!info.appearedAnswer)
    {
        addXe(true, info);
    }
    
    Info _info_false = DataImage::getInstance()->randomQuestion();
    addXe(false, _info_false);
    

    type++;
    
}

void SheepLayer::updateScore()
{
    lbScore->setString(std::to_string(score));
}
void SheepLayer::addXe(bool ts, Info info)
{
    Vector<Node*> _listxe;
    
    auto ro0 = Ro::create();
   
    ro0->runAction(Xe::runing());
    
    if(ts)
    {
        ro0->setPosition(_size.width , 240);
        
        
       
    }
    else
    {
         ro0->setPosition(_size.width + ro0->getContentSize().width * 1.2 , 240);
    }
    
    
    this->addChild(ro0,9999);
    _listxe.pushBack(ro0);
    
    Label* w0 = Label::createWithTTF(info.answer,"Marker Felt.ttf", 30);
    w0->setColor(Color3B(255, 255, 255));
    w0->setPosition(ro0->getPositionX()- 30, ro0->getPositionY() + ro0->getContentSize().height / 2 - w0->getContentSize().height  - 10);
    this->addChild(w0,9999);
    _listxe.pushBack(w0);
    
    Xe *_xe = new Xe(_listxe);
    
    

    _xe->set_ID(info.ID);
    
    listXe.pushBack(_xe);
    _listxe.clear();

}

void SheepLayer::actionSheep()
{
    
    
    for(auto _sheep : *listSheep)
    {
        if(_sheep->sheepStaus == isHit)
            return;
        
        for(auto _pic :*listPic)
        {
            if(_pic->get_ID() == _sheep->get_ID())
                _pic->setPosition(_sheep->getPositionX(), _sheep->getPositionY() - _sheep->getContentSize().height / 2);
        }
    
        if(_sheep->getPositionY() <= GameScene::getInstance()->land->getPositionY() + _sheep->getContentSize().height &&_sheep->sheepStaus == isNormal)
        {
            _sheep->sheepStaus = isHit;
             CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(SOUND_HIT);
            GameScene::getInstance()->isGameOver = true;
            DataImage::getInstance()->changeRate(_sheep->get_ID(), 1000);
            DataImage::getInstance()->writeFile("Data2.csv", ',');
            
        }
        
        if(_sheep->velocY <= 0)
        {
            _sheep->velocY = 0.0;
            _sheep->directionY = 1;
            _sheep->sheepStaus = isNormal;
            //_sheep->runAction(RotateTo::create(0.6, 30));
        }
        
//        if(_sheep->getPositionY() <= GameScene::getInstance()->land->getContentSize().height + _sheep->getContentSize().height / 2)
//        {
//           
//            _sheep->setPositionY(GameScene::getInstance()->land->getContentSize().height + _sheep->getContentSize().height /2 );
//        }
        
        _sheep->velocY += _sheep->directionY * _sheep->getDeltaA();
        
        _sheep->setPositionY(_sheep->getPositionY() - _sheep->velocY * _sheep->directionY);
        _sheep->setPositionX(_sheep->getPositionX() + 1.0 * _sheep->directionX);
    }
    
   
}

void SheepLayer::cleanUp()
{
    for (auto _sheep : *listSheep )
    {
        listSheep->eraseObject(_sheep);
    
    }
    
    delete listSheep;
    listSheep = nullptr;
    
}

void SheepLayer::getTapAction(cocos2d::Vec2 pos)
{
    
    
    for(auto _sheep : *listSheep)
    {
        
        _sheep->setTapLocation(pos);
        
        if(_sheep->isTap(_sheep->getPosition()) && _sheep->sheepStaus == isNormal)
        {
            CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(SOUND_CLICK);
            _sheep->sheepStaus = isTap;
            _sheep->directionY = -1;
            _sheep->velocY = 10.0;
            //_sheep->runAction(RotateTo::create(0.2, -30));
            count++;
            
            if(count == 5)
            {
                addSheep();
                break;
            }

            if(count == 12)
            {
                addSheep();
                break;
            }

            if(count == 18)
            {
                addSheep();
                break;
            }
            
            if(count == 25)
            {
                addSheep();
                break;
            }

            
        }
    }
}










